package com.ftg.learn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.entity.StudentEntity;
import com.ftg.learn.service.StudentService;
import com.ftg.learn.util.CodeStatusEnum;
import com.ftg.learn.util.RespMessUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * 控制层
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/8 上午9:21
 */
@Api(tags = "学生模块")
 //cros跨域没有拦截
@CrossOrigin(value = "http://192.168.1.56:9090",methods = RequestMethod.GET)
@RestController
public class StudentController {

    @Autowired
    @Qualifier("aa")
    StudentService studentService;

    @ApiOperation(value = "展示全部学生信息")
    @GetMapping("/student/showall")
    public RespMessUtil<IPage<StudentEntity>> showall(
            @ApiParam(value = "用户", name = "name") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "当前页码", name = "current", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current,
            @ApiParam(value = "显示条数", name = "limit", required = true) @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ) {
        IPage<StudentEntity> ipage = new Page<>(current, limit);

        studentService.pageWithName(name, current, limit, ipage);

        return RespMessUtil.build(CodeStatusEnum.SUCCESS, ipage);
    }

    @ApiOperation(value = "查询某一个学生信息")
    @GetMapping("/student/findone")
    public RespMessUtil<StudentEntity> findone(@RequestParam("sno") String sno) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, studentService.findone(sno));
    }

    @ApiOperation(value = "部学生信息添加")
    @PostMapping("/student/save")
    public RespMessUtil<Integer> insertStudent(@RequestBody StudentEntity se) {
        int n = studentService.insertStudent(se);
        return n > 0 ? RespMessUtil.insertOk() : RespMessUtil.insertFailure();
    }

}
